package com.kandinfo.survey.common.service.impl;

import java.util.Date;

import javax.annotation.Resource;

import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.kandinfo.base.common.service.AbstractService;
import com.kandinfo.base.common.util.DateUtils;
import com.kandinfo.base.security.domain.User;
import com.kandinfo.survey.common.constants.StatusConstant;
import com.kandinfo.survey.common.domain.UserAnswerInfo;
import com.kandinfo.survey.common.repository.UserAnswerInfoRepository;
import com.kandinfo.survey.common.service.UserAnswerInfoService;

@Service
public class UserAnswerInfoServiceImpl extends AbstractService<UserAnswerInfo, Long, UserAnswerInfoRepository>
        implements UserAnswerInfoService {
    
    @Resource
    public void setUserAnswerInfoRepository(UserAnswerInfoRepository userAnswerInfoRepository) {
        super.setRepository(userAnswerInfoRepository);
    }

    @Override
    @Transactional
    public UserAnswerInfo saveUserAnswerInfo(User curUser, Long questionnaireId) {
        UserAnswerInfo info = new UserAnswerInfo();
        info.setAnswerDate(DateUtils.format(new Date(), DateUtils.DATE_PATTERN_YMD));
        info.setCreateBy(curUser.getId());
        info.setCreateTs(new Date());
        info.setUserId(curUser.getId());
        info.setQuestionnaireId(questionnaireId);
        info.setStatus(StatusConstant.NORMALLY_USE);

        repository.saveAndFlush(info);
        return info;
    }

}
